﻿/*            UITools            */
(function ($) {
    $.fn.hint = function () {
        return this.each(function () {
            var t = $(this);
            var title = t.attr('title');

            if (title) {
                t.focus(function () {
                    if (t.val() == title) {
                        t.val('');
                        t.addClass('blur');
                    }
                })

                t.blur(function () {
                    if (t.val() == '') {
                        t.val(title);
                        t.removeClass('blur');
                    }
                })

                t.blur();
            }
        })
    }
})(jQuery);
/*            UITools            */

/*            PreLoading            */
(function ($) {
    $.fn.extend({
        PreLoading: function (options) {
            var defaults = {
                forceLoadTime: 2500,
                fadeInTime: 350
            };

            var options = $.extend(defaults, options);

            return this.each(function () {
                var o = options;
                var obj = $(this);

                setTimeout(function () {
                    showPage(obj, o);
                }, o.forceLoadTime);

                showPage(obj, o);
            });
        }
    });
})(jQuery);

function showPage(obj, o) {
    if ($('.preloadimage').is(":visible")) {
        $('.preloadimage').hide();
        obj.fadeIn(o.fadeInTime);
    }
}
/*            PreLoading            */

/*            StarRating            */
(function ($) {
    $.fn.extend({
        StarRating: function (options) {
            var defaults = {
                currentPage: "",
                currentRate: 3,
                isClicked: false,
                isRateSend: false,
                imageUrl: "",
                ajaxRatePage: ""
            };

            var options = $.extend(defaults, options);

            return this.each(function () {
                var o = options;
                var obj = $(this);
                var items = $("li", obj);

                SetSelected($(this), items, o.currentRate, o);
                items.css({ 'background-image': 'url("' + o.imageUrl + '")' });

                items.mouseover(function () {
                    if (o.isRateSend)
                        return;

                    var index = $("li", obj).index($(this));
                    SetHover(obj, items, index, o);
                }).mouseout(function () {
                    if (o.isRateSend)
                        return;

                    var index = $("li", obj).index($(this));
                    if (o.isClicked)
                        SetHover(obj, items, index, o);
                    else
                        SetSelected(obj, items, o.currentRate, o);
                }).click(function () {
                    if (o.isRateSend)
                        return;

                    o.currentRate = $("li", obj).index($(this));
                    o.isClicked = true;
                    SetHover(obj, items, o.currentRate, o);
                });
            });
        }
    });
})(jQuery);

function SetHover(obj, items, index, options) {
    index = Math.round(index);

    for (var i = 0; i < items.length; i++) {
        $(items[i]).removeClass("selected");
        if (i <= index) {
            $(items[i]).addClass("hover");
            if (!options.isRateSend && options.isClicked) {
                SendRate(items, options);
            }
        }
        else
            $(items[i]).removeClass("hover");
    }
}

function SetSelected(obj, items, index, options) {
    index = Math.round(index);

    for (var i = 0; i < items.length; i++) {
        $(items[i]).removeClass("hover");
        if (i <= index)
            $(items[i]).addClass("selected");
        else
            $(items[i]).removeClass("selected");
    }
}

function SendRate(items, options) {
    $.post(options.ajaxRatePage,
            {
                Guid: options.currentPage,
                Rate: options.currentRate
            });

    for (var i = 0; i < items.length; i++) {
        $(items[i]).css({ "cursor": "default" });
    }
    options.isRateSend = true;
}
/*            StarRating            */

/*            Menu            */
$(function () {
    if (typeof (MenuId) == "undefined")
        return;

    var titles = PageUrl.split('/');
    var lis = $("li", $("#" + MenuId));
    for (var i = 0; i < lis.length; i++)
        for (var j = 0; j < titles.length; j++) {
            if ($(lis[i]).attr("title") == titles[j]) {
                $(lis[i]).addClass("selected");
                break;
            }
            else
                $(lis[i]).removeClass("selected");
        }

//    $('#' + MenuId + ' ul > li').mouseenter(function () {
//        //$(this).addClass('hover');
//        // check width	
//        var $ul = $(this).find('ul');
//        var $link = $(this).find('a:first');

//        // if link is wider make the list the same width
//        if ($ul.width() < $link.outerWidth()) {
//            $ul.width($link.outerWidth());
//        }

//        // ie7 nonsense
//        if (ie7) { $ul.find('li').width($ul.outerWidth()) };

//    }).mouseleave(function () {
//        //$(this).removeClass('hover');
//        Cufon.refresh();
//    });
});
/*            Menu            */

/*            Slider            */
$(function () {
    if (typeof (SliderID) == "undefined")
        return;
    $('#slider').nivoSlider({
        effect: 'sliceDown',
        captionOpacity: 0,
        slices: 8,
        animSpeed: 600,
        controlNav: true,
        controlNavThumbs: false,
        controlNavThumbsFromRel: false,
        pauseTime: pauseTime,
        beforeChange: function () {
            $('.nivo-imageLink:visible span').animate({ 'marginLeft': '0', 'opacity': '0' }, "fast", "swing");
        },
        afterChange: function () {
            $('.nivo-imageLink:visible span').animate({ 'marginLeft': '-20px', 'opacity': '1' }, "fast", "swing");
        }
    });

    $('.nivo-imageLink:first span').animate({ 'marginLeft': '-20px', 'opacity': '1' }, "fast", "swing");
});
/*            Slider            */

/*            SubMenu            */
$(function () {
    if (typeof (subMenu) == "undefined")
        return;
    $('li a', $(subMenu)).live('click', function (e) {
        var selectedMenu = $(e.target);

        $('li', $(subMenu)).removeClass('selected');
        selectedMenu.parent().addClass('selected');

        $("li", $(work)).each(function (index) {
            if (selectedMenu.attr('title') == 'All') {
                $("li", $(work)).fadeIn();
                return;
            }

            if ($(this).hasClass(selectedMenu.attr('title')))
                $(this).fadeIn();
            else
                $(this).fadeOut();
        });

        return;
    });
});
/*            SubMenu            */

/*            GoogleAnalytics            */
$(function () {
    if (typeof (TrackerID) == "undefined")
        return;

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', TrackerID]);
    _gaq.push(['_trackPageview']);

    (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
});
/*            GoogleAnalytics            */

/*            TwitterStatus            */
function twitterCallback(twitters) {
    var statusHTML = [];
    statusHTML.push("<ul class='List'>");
    for (var i = 0; i < twitters.length; i++) {
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function (url) {
            return '<a target="_blank"  href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function (reply) {
            return reply.charAt(0) + '<a target="_blank"  href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<li>' + status + ' <a href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '">' + relative_time(twitters[i].created_at) + '</a></li>');
    }
    statusHTML.push("</ul>");
    $(function () {
        document.getElementById(divTwitterStatus).innerHTML = statusHTML.join('');
    });
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}
/*            TwitterStatus            */
